<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-03-14 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'change_order':
				$this->changeOrder();
				break;
			case 'do_change_order':
				$this->doChangeOrder();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{		
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		$bname = isset($this->request['bname']) ? $this->request['bname'] : '';
		
		$seafood = get_seafood(array('sid'=>$sid));
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("请指定海鲜", 'manage.php?m=seafood');
		}
		$sname = $seafood['name'];
				
		$list = get_seafood_recommend_list(array('sid'=>$sid));
				
		include template('admin/seafood_recommend');
	}
	
	private function add()
	{		
		$page = $this->request['offset'] + 1;
		
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		$bname = isset($this->request['bname']) ? $this->request['bname'] : '';
		
		$seafood = get_seafood(array('sid'=>$sid));
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("请指定海鲜", 'manage.php?m=seafood');
		}
		$sname = $seafood['name'];
		
		$query_link = "manage.php?m=seafood_recommend&a=add&sid=$sid&bname=$bname";
		
		$list = get_seafood_recommend_list(array('sid'=>$sid));
		$r_ids = array();
		foreach($list as $temp)
		{
			$r_ids[] = $temp['business_id'];
		}
		
		$this->request['name'] = $bname;
		$result = get_business_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
				
		$action_name = '添加海鲜推荐商家';
		include template('admin/seafood_recommend_add');
	}
	
	private function doAdd()
	{
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';

		$redirect = 'manage.php?m=seafood_recommend&a=add&sid='.$sid;
		
		if(!$ids)
		{
			$this->messager("请指定商家", $redirect);
		}
		
		$seafood = get_seafood(array('sid'=>$sid));
		
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("请指定海鲜", 'manage.php?m=seafood');
		}
		
		$list = get_seafood_recommend_list(array('sid'=>$sid));
		$r_ids = array();
		foreach($list as $temp)
		{
			$r_ids[] = $temp['business_id'];
		}
		
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				if(!in_array($id, $r_ids))
				{
					$result = add_seafood_recommend(array('uid'=>$this->user['id'], 'sid'=>$sid, 'bid'=>$id));
				}
			}
		}
		else
		{
			if(!in_array($ids, $r_ids))
			{
				$result = add_seafood_recommend(array('uid'=>$this->user['id'], 'sid'=>$sid, 'bid'=>$ids));
			}
		}
		
		if($result)
		{
			$this->messager("添加成功", $redirect);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function changeOrder()
	{
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		
		$seafood = get_seafood(array('sid'=>$sid));
		
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("请指定海鲜", 'manage.php?m=seafood');
		}
		
		$list = get_seafood_recommend_list(array('sid'=>$sid));
		
		include template('admin/seafood_recommend_order');
	}
	
	private function doChangeOrder()
	{	
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		$orders = isset($this->request['orders']) ? $this->request['orders'] : '';

		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $i=>$id)
			{
				$result = update_seafood_recommed_order(array('id'=>$id, 'order'=>$orders[$i]));
			}
		}
				
		if($result)
		{
			$this->messager("修改成功", 'manage.php?m=seafood_recommend&a=change_order&sid='.$sid);
		}
		else
		{
			$this->messager("修改失败", 'manage.php?m=seafood_recommend&a=change_order&sid='.$sid);
		}
	}
	
	private function doDelete()
	{		
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';

		$redirect = 'manage.php?m=seafood_recommend&sid='.$sid;
		
		if(!$ids)
		{
			$this->messager("请指定商家", $redirect);
		}
		
		$seafood = get_seafood(array('sid'=>$sid));
		
		if(!isset($seafood['id']) || $seafood['id'] < 1)
		{
			$this->messager("请指定海鲜", 'manage.php?m=seafood');
		}
		
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				$result = delete_seafood_recommend(array('sid'=>$sid, 'bid'=>$id));
			}
		}
		else
		{
			$result = delete_seafood_recommend(array('sid'=>$sid, 'bid'=>$ids));
		}
		
		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
}